package com.fangyi.yunmusic.service.impl;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.mapper.AlbumMapper;
import com.fangyi.yunmusic.mapper.ArtistHasAlbumMapper;
import com.fangyi.yunmusic.mapper.ArtistMapper;
import com.fangyi.yunmusic.mapper.UserMapper;
import com.fangyi.yunmusic.pojo.Album;
import com.fangyi.yunmusic.pojo.ArtistHasAlbum;
import com.fangyi.yunmusic.pojo.front.end.FEAlbumVO;
import com.fangyi.yunmusic.pojo.vo.AlbumDetailVO;
import com.fangyi.yunmusic.pojo.vo.AlbumVO;
import com.fangyi.yunmusic.service.AlbumService;
import com.fangyi.yunmusic.utils.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.n3r.idworker.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

import static java.util.stream.Collectors.toList;
@Service
public class AlbumServiceImpl implements AlbumService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArtistMapper artistMapper;
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private ArtistHasAlbumMapper artistHasAlbumMapper;
    @Autowired
    private ResourceConfig resourceConfig;
    @Autowired
    private SongServiceImpl songService;
    @Override
    public void insertAlbum(String artistId, FEAlbumVO feAlbumVO) {
        if(artistMapper.selectByPrimaryKey(artistId)==null)
            throw new YunMusicException("歌手ID不存在！");
        Album album=new Album();
        BeanUtils.copyProperties(feAlbumVO,album);
        String albumId=Id.nextString();
        album.setId(albumId);
        album.setPublishTime(new Date());
        albumMapper.insert(album);
        ArtistHasAlbum artistHasAlbum=new ArtistHasAlbum();
        artistHasAlbum.setId(Id.nextString());
        artistHasAlbum.setArtistId(artistId);
        artistHasAlbum.setAlbumId(albumId);
        artistHasAlbumMapper.insert(artistHasAlbum);

    }

    @Override
    public void deleteAlbum(String albumId) {
        if(albumMapper.selectByPrimaryKey(albumId)==null)
            throw new YunMusicException("专辑ID不存在！");
        albumMapper.deleteByPrimaryKey(albumId);
        Example example=new Example(ArtistHasAlbum.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("albumId",albumId);
        List<ArtistHasAlbum> artistHasAlbums=artistHasAlbumMapper.selectByExample(example);
        if(artistHasAlbums.size()!=1)
            throw new YunMusicException("歌手专辑关系表对应关系出错");
        artistHasAlbumMapper.deleteByPrimaryKey(artistHasAlbums.get(0).getId());
//        artistMapper.deleteByPrimaryKey(artistHasAlbums.get(0).getArtistId());
    }

    @Override
    public PagedResult selectAlbum(String artistId,int page, int pageSize) {
        Example example=new Example(ArtistHasAlbum.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("artistId",artistId);
        List<String> albumIds=artistHasAlbumMapper.selectByExample(example).stream().map(ArtistHasAlbum::getArtistId).collect(toList());

        PageHelper.startPage(page,pageSize);
//        List<Album> albumList=albumMapper.selectAll();
        List<Album> albumList=albumMapper.selectAlbumByIds(albumIds);
        return PagedResult.pagedResult(albumList);
    }

    @Override
    public PagedResult selectAlbum(int page, int pageSize) {
        Example example = new Example(Album.class);
        example.orderBy("publishTime").desc();
        PageHelper.startPage(page, pageSize);
        List<Album> albums = albumMapper.selectByExample(example);
        PageInfo<?> pageInfo = new PageInfo<>(albums);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        List<AlbumVO> albumVOList = convertToAlbumVOs(albums);
        return new PagedResult(albumVOList, page, pages, total);
    }

    @Override
    public PagedResult selectAlbumVO(String artistId,Integer page, Integer pageSize) {
        if(artistMapper.selectByPrimaryKey(artistId)==null)
            throw new YunMusicException("歌手ID不存在！");
        Example example=new Example(ArtistHasAlbum.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("artistId",artistId);
        List<String> albumIds=artistHasAlbumMapper.selectByExample(example).stream().map(ArtistHasAlbum::getAlbumId).collect(toList());

        PageHelper.startPage(page,pageSize);
//        List<Album> albumList=albumMapper.selectAll();
        List<Album> albumList=albumMapper.selectAlbumByIds(albumIds);
        PageInfo<?> pageInfo=new PageInfo<>(albumList);
        int pages=pageInfo.getPages();
        long total= pageInfo.getTotal();
        List<AlbumVO> albumVOList=convertToAlbumVOs(albumList);
        return new PagedResult(albumVOList,page,pages,total);
}

    @Override
    public AlbumDetailVO getAlbumDetailVO(String albumId) {
        if(albumMapper.selectByPrimaryKey(albumId)==null)
            throw new YunMusicException("专辑ID不存在！");
        Album album=albumMapper.selectByPrimaryKey(albumId);
        AlbumDetailVO albumDetailVO=new AlbumDetailVO();
        BeanUtils.copyProperties(album,albumDetailVO);
        albumDetailVO.setPicUrl(resourceConfig.getAlbumCoverUrl(album.getPicUrlId()));
        albumDetailVO.setSongVOList(songService.selectSongByAlbumId(albumId));
        return albumDetailVO;
    }

    public List<AlbumVO> convertToAlbumVOs(List<Album> albumList){
        return albumList.stream().map(album -> {
            AlbumVO albumVO=new AlbumVO();
            BeanUtils.copyProperties(album,albumVO);
            albumVO.setPicUrl(resourceConfig.getAlbumCoverUrl(album.getPicUrlId()));
            return albumVO;
        }).collect(toList());

    }

    @Override
    public Integer getAlbumCount() {
        return albumMapper.getAlbumCount();
    }
}
